package com.lightsh.jbox2d;

import com.lightsh.graphics.ImageUtility;
import com.lightsh.gui.BaseGameActivity;
import com.lightsh.gui.PhysicsScreen;
import com.lightsh.screens.ObjectTestScreen;
import com.lightsh.screens.PlaneScreen;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.Menu;
import android.view.MenuItem;

public class GameActivity extends BaseGameActivity{
	public static final String EXTRA_TARGET_SCREEN_NAME = "ExtraTargetScreenName";
	@Override
	protected void initScreen() {
		
		Bundle bundle = getIntent().getExtras();
		if(bundle == null || !bundle.containsKey(EXTRA_TARGET_SCREEN_NAME)){
			throw new RuntimeException("you must supply a screen class");
		}
		
		int screenW = getWindowManager().getDefaultDisplay().getWidth();
		int screenH = getWindowManager().getDefaultDisplay().getHeight();		
		String key = bundle.getString(EXTRA_TARGET_SCREEN_NAME);
		if(key.equals(PlaneScreen.class.getName())){
			mScreen = new PlaneScreen(this, screenW, screenH);
			mScreen.setBackgroundBitmap(ImageUtility.drawableToBitmap(getResources().getDrawable(R.drawable.background)));
		}else if(key.equals(ObjectTestScreen.class.getName())){
			mScreen = new ObjectTestScreen(this, screenW, screenH);
		}else{
			throw new IllegalArgumentException("unknow screen class!");
		}
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		boolean handle = super.onCreateOptionsMenu(menu);
		if(mScreen instanceof PlaneScreen){
			menu.add(0, 1, 1, "");
			menu.add(0, 2, 2, "");
			menu.add(0, 3, 3, getString(R.string.restart));
		}
		return handle;
	}
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		if(mScreen instanceof PlaneScreen){
			MenuItem item = menu.getItem(1);
			PlaneScreen screen = (PlaneScreen) mScreen;
			item.setTitle(screen.isSensorControl() ? getString(R.string.keyControl) : getString(R.string.sensorControl));
			
			item = menu.getItem(2);
			if(screen.isEnd()){
				item.setVisible(false);
			}else{
				item.setVisible(true);
				item.setTitle(screen.isContactable() ? getString(R.string.unstoppable) : getString(R.string.cancelUnstoppable));
			}
			
			if(screen.getCurrentRectClone().top != 0){
				screen.pause();
			}
			
			item = menu.getItem(3);
			if(screen.isEnd()){
				item.setVisible(true);
			}else{
				item.setVisible(false);
			}
			
		}
		boolean handle = super.onPrepareOptionsMenu(menu);
		return handle;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(mScreen instanceof PlaneScreen){
			switch(item.getItemId()){
			case 1:{
				PlaneScreen screen = (PlaneScreen) mScreen;
				screen.enableSensorControl(!screen.isSensorControl());
				return true;
			}
			case 2:{
				PlaneScreen screen = (PlaneScreen) mScreen;
				screen.enableContact(!screen.isContactable());
				return true;
			}
			case 3:{
				PlaneScreen screen = (PlaneScreen) mScreen;
				screen.restartGame();
				return true;
			}
			}
		}
		return super.onOptionsItemSelected(item);
	}
}
